<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>

<div id="updateDialog" class="container-fluid">
    <div class="row">
        <form id="updateForm" method="post" class="form-horizontal" >
            <div class="form-group">
                <label class="col-sm-4 control-label" for="title">
                    <span class="help-inline"><font color="red">*</font> </span>
                    标题：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="title" name="title" placeholder="标题" value="${wechatMpArticle.title}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="jwid">
                    归属公号：</label>
                <div class="col-sm-5">
                    <select id="jwid" name="jwid" class="form-control" placeholder="归属公号">
                        <c:forEach var="wechatJwid" items="${wechatMpWebJwidList}">
                            <option value="${wechatJwid.jwid}" <c:if test="${wechatMpArticle.jwid==wechatJwid.jwid}">selected="selected"</c:if>>${wechatJwid.name}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="content">内容：</label>
                <div class="col-sm-5">
                    <textarea class="form-control" id="content" name="content" placeholder="内容">${wechatMpArticle.content}</textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="orders">排序号：</label>
                <div class="col-sm-5">
                    <input type="number"  class="form-control" id="orders" name="orders" placeholder="排序号" value="${wechatMpArticle.orders}"></input>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="description">描述：</label>
                <div class="col-sm-5">
                    <textarea class="form-control" id="description" name="description" placeholder="描述">${wechatMpArticle.description}</textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-9 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="updateDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    function validateForm(){
        return $( "#updateForm" ).validate( {
            rules: {
                title: {
                    required: true,
                    maxlength: 100
                },
                content: {
                    maxlength: 4000
                },
                description: {
                    maxlength: 500
                },
                orders: {
                    maxlength: 10
                }
            },
            messages: {
                title: {
                    required: "该项是必填项。",
                    maxlength: "长度超出100个字符。"
                },
                content: {
                    maxlength: "长度超出4000个字符"
                },
                orders: {
                    maxlength: "长度超出11个字符"
                },
                description: {
                    maxlength: "长度超出500个字符"
                }
            },
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }
    //注册表单验证
    $(validateForm());

    <%--$(function(){--%>
        <%--//初始化树形select控件--%>
        <%--$("#agent").bootstrapCombotree({--%>
            <%--data: ${upmsAgents},//data应符合实例的data格式--%>
            <%--defaultLable:"请选择",--%>
            <%--name : 'pid'//combotree值域的name，可以用在表单提交--%>
        <%--})--%>
    <%--});--%>

    function createSubmit() {
        if(validateForm().form()){
            $.ajax({dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/article/update/${wechatMpArticle.id}',
                data: $('#updateForm').serialize(),
                success: function(result) {
                    if (result.code != 1) {
                        if (result.data instanceof Array) {
                            $.each(result.data, function(index, value) {
                                infoDialog(value.errorMsg);
                            });
                        } else {
                            infoDialog(result.data);
                        }
                    } else {
                        updateDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    infoDialog(textStatus);
                }
            });
        }
    }
</script>